package com.arron.algorithm.leetcodetop100.栈;


import java.util.LinkedList;
import java.util.Stack;

/**
 *  394. 字符串解码
 */
public class 字符串解码 {


    Stack<Character> stack = new Stack<>();


    public String decodeString_plus(String s) {
        StringBuffer res = new StringBuffer();

        LinkedList<String> res_stack = new LinkedList<>();
        LinkedList<Integer> multi_stack = new LinkedList<>();
        int multi = 0;
        for (char c : s.toCharArray()) {

            if (c >= '0' && c<= '9'){
                multi = multi *10 + Integer.parseInt(c+"");
            }else if(c == '['){
                multi_stack.addLast(multi);
                multi = 0;
//                if (res.length()>0){ 这个是多余的
                // 这里用的精妙绝伦 ，和43，44行对应
                    res_stack.addLast(res.toString());
                    res = new StringBuffer();
//                }
            }else if (c==']'){

                StringBuffer repStr = new StringBuffer();

                Integer count = multi_stack.removeLast();
                for (int i = 0; i < count; i++) {
                    repStr.append(res);
                }

                String last = res_stack.pollLast();
                 res = new StringBuffer(last== null ? "":last).append(repStr);

            }else {
                res.append(c);

            }
        }
        return res.toString();
    }





    public String decodeString(String s) {

        char[] chars = s.toCharArray();

        StringBuffer res = new StringBuffer();

        for (char c : chars) {

            if (c != ']'){
                stack.push(c);
            }else {
                //拿出重复的字符串

                StringBuffer sb = new StringBuffer();
                while (!stack.isEmpty() && stack.peek() != '['){
                    sb.append(stack.pop());
                }
                //去除多余的[
                stack.pop();
                //拿到重复的次数
                int count = getCount();
                StringBuffer sb02 = new StringBuffer();
                for (int i = 0; i < count; i++) {
                    sb02.append(sb);
                }

                //将解码后的字符又放回栈中
                for (char c1 : sb02.reverse().toString().toCharArray()) {
                    stack.push(c1);
                }

            }
        }

        while (!stack.isEmpty()){

            res.append(stack.pop());
        }


    return res.reverse().toString();
    }



    private Integer getCount(){
        StringBuffer sb = new StringBuffer();
        while (!stack.isEmpty() && Character.isDigit(stack.peek())){
            sb.append(stack.pop());
        }
        return Integer.parseInt(sb.reverse().toString());
    }


    public static void main(String[] args) {
        字符串解码 jm = new 字符串解码();
        String res = "zzzyypqjkjkefjkjkefjkjkefjkjkefyypqjkjkefjkjkefjkjkefjkjkefef";
        String s= "3[z]2[2[y]pq4[2[jk]e1[f]]]ef";
        System.out.println("s == "+jm.decodeString_plus(s));
        System.out.println("res == "+res.equals(jm.decodeString_plus(s)));
    }

}
